<?php

// 广告位表

namespace app\console\model;
use think\Db;
use think\Model;
use think\Request;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Session;
use think\Hook;

class ServiceOrderEvaluate extends Model
{
    protected $table = 'sun_order_evaluate';
    // 模型事件
    protected static function init()
    {

        self::event('before_write', function ($data) {
            if (\request()->action() == 'create') {
                //有添加操作就是有权限 //有可能创建人就是审核人
                $data->create_time = date('Y-m-d H:i:s', time());
                $data->update_time = date('Y-m-d H:i:s', time());
                $data->create_id = Session::get('manage_id');
                $data->reason = '';

                if (isset($data->auding_status)) { //有审核状态 审核权限人
                    //多次审核
                    $role_id = session('role_id');
                    //管理员2 一级审核员11 二级审核员12 三级审核员13
                    if($role_id == 2 || $role_id == 11){
                        $data->auding_status1 = $data->auding_status;

                    }
                    if($role_id == 12){
                        $data->auding_status2 = $data->auding_status;
                    }
                    if($role_id == 13){
                        $data->auding_status3 = $data->auding_status;
                    }
                    $data->auding_status = 1;
                }

            } else {

                if (isset($data->auding_status)) { //有审核状态 审核权限人 2是审核成功
                    //多次审核
                    $role_id = session('role_id');
                    //管理员2 一级审核员11 二级审核员12 三级审核员13
                    $status = false;
                    $auding_status1 = Db::name('order_evaluate')->where('id',$data->id)->value('auding_status1');
                    $auding_status2 = Db::name('order_evaluate')->where('id',$data->id)->value('auding_status2');
                    $auding_status3 = Db::name('order_evaluate')->where('id',$data->id)->value('auding_status3');
                    if($role_id == 2 || $role_id == 11){
                        $data->auding_status1 = $data->auding_status;
                        if($data->auding_status == 2 && $auding_status2 == 2 && $auding_status3 ==2){ //这有这一种情况是审核通过 其他全部是失败
                            $status = true;
                        }

                    }
                    if($role_id == 12){ //2级审核
                        if($auding_status1 != 2){
                            fail('请等一级审核完毕');
                        }
                        $data->auding_status2 = $data->auding_status;
                        if($data->auding_status == 2 && $auding_status1 == 2 && $auding_status3 ==2){ //这有这一种情况是审核通过 其他全部是失败
                            $status = true;
                        }
                    }
                    if($role_id == 13){
                        if($auding_status2 != 2 || $auding_status1 != 2){
                            fail('请等一、二级审核完毕');
                        }
                        $data->auding_status3 = $data->auding_status;
                        if($data->auding_status == 2 && $auding_status1 == 2 && $auding_status2 ==2){ //这有这一种情况是审核通过 其他全部是失败
                            $status = true;
                        }
                    }
                    if($role_id == 1){
                        if($data->auding_status == 2){ //审核通过
                            $status = true;
                            $data->auding_status1 = 2;
                            $data->auding_status2 = 2;
                            $data->auding_status3 = 2;
                        }else{
                            $data->auding_status1 = 1;
                            $data->auding_status2 = 1;
                            $data->auding_status3 = 1;
                        }

                    }
                    if($status == true){
                        $data->auding_status = 2;
                    }else{
                        // $data->auding_status = $data->auding_status;使用默认提交值
                        $data->auding_status = 3; //必须失败 否则 默认提交过来的auding_status 自动还是变成 1 2 3 状态;
                    }

                }

            }
        });
    }



    public function getAudingTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status']];
    }

    public function getAudingText1Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status1']];
    }
    public function getAudingText2Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status2']];
    }
    public function getAudingText3Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status3']];
    }
//


//    public function getServiceTypeAttr($value, $data)
//    {
//        $ret = Db::name('single')->where('id',$data['single_id'])->value('cat_name');
//        return $ret;
//    }
    public function getUsernameAttr($value, $data)
    {
        $ret = Db::name('member')->where('id',$data['member_id'])->value('username');
        return $ret;
    }

}